package com.woniu.command.handle;

import com.sun.org.apache.bcel.internal.generic.I2F;
import com.woniu.adapter.ProSchedulingAdapter;
import com.woniu.client.WorkClient;
import com.woniu.inlet.vo.WorkingProcedureVo;
import com.woniu.outlet.dao.OrderDetailsPoMapper;
import com.woniu.outlet.dao.ProPlanPoMapper;
import com.woniu.outlet.po.ProPlanPo;
import com.woniu.outlet.po.ProPlanPoExample;
import com.woniu.outlet.po.ProSchedulingPo;
import com.woniu.service.ProSchedulingPoService;
import com.woniu.uitl.ProPlanStatus;
import com.woniu.uitl.ProScheduingStatus;
import com.woniu.uitl.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

@Component
@Transactional
public class ProSchedulingCommandHandle {

    @Autowired
    private ProSchedulingAdapter proSchedulingAdapter;
    @Autowired
    private ProPlanPoMapper proPlanPoMapper;
    @Autowired
    private OrderDetailsPoMapper orderDetailsPoMapper;
    @Autowired
    private ProSchedulingPoService proSchedulingPoService;
    @Autowired
    private WorkClient systemClient;
    //添加生产排程
    public String addProSche(ProSchedulingPo po){

        //判断生产计划单号状态是否为已完成
        ProPlanPoExample poExample = new ProPlanPoExample();
        ProPlanPoExample.Criteria criteria = poExample.createCriteria();
        criteria.andIdEqualTo(po.getProPlanId());

        List<ProPlanPo> poList = proPlanPoMapper.selectByExample(poExample);
        if (poList.size() == 0){
            return "该生产计划不存在，请选择其它生产计划重新排程";
        }
        if (poList.get(0).getStatus().equals(ProPlanStatus.COMPLETE.getStatus())){
            return "该生产计划已经全部完成,请选择其它生产计划重新排程";
        }
        //判断生产计划下的某个产品是否为已完成
        String status = orderDetailsPoMapper.getStatus(po.getProPlanId(),po.getProName());
        if (StringUtils.isEmpty(status)){
            return "该生产计划下并没有此产品,请选择其它产品重新排程";
        }
        if (!status.equals("未完成")){
            return "该产品已经"+status+",请选择其它产品进行排程";
        }
        if (po.getWorkCodes().size() != 0){
            for (int i = 0; i < po.getWorkCodes().size(); i++) {
                ResponseResult<WorkingProcedureVo> workerByCode = systemClient.getByCode(po.getWorkCodes().get(i));
                if (StringUtils.isEmpty(workerByCode.getData())){
                    po.getWorkCodes().remove(i);
                }
            }
        }
        Integer odId = orderDetailsPoMapper.getId(po.getProPlanId(),po.getProName());
        String sta = "已排产";
        try {
            proSchedulingAdapter.addProSche(po);
            orderDetailsPoMapper.update(odId,sta);
            return "添加成功,请尽快提交";
        } catch (Exception e) {
            e.printStackTrace();
            return "添加失败"+e.getMessage();
        }
    }

    //删除生产排程
    public String delete(Long id){
        try {
            proSchedulingAdapter.deleteProSche(id);
            return "删除成功";
        } catch (Exception e) {
            e.printStackTrace();
            return "删除失败"+e.getMessage();
        }
    }

    //修改生产排程
    public String update(ProSchedulingPo po){
        ProSchedulingPo proScheById = null;
        try {
            proScheById = proSchedulingPoService.getProScheById(po.getId());
        } catch (Exception e) {
            e.printStackTrace();
            return "无效的生产排程";
        }
        if (!proScheById.getStatus().equals(ProScheduingStatus.UNSUBMITTED.getStatus())){
            return "该生产排程处于"+proScheById.getStatus()+"状态,请选择其它产品进行排程修改";
        }
        //判断生产计划单号状态是否为已完成
        ProPlanPoExample poExample = new ProPlanPoExample();
        ProPlanPoExample.Criteria criteria = poExample.createCriteria();
        criteria.andPlanNoEqualTo(po.getProPlanId());
        List<ProPlanPo> poList = proPlanPoMapper.selectByExample(poExample);
        if (poList.size() == 0){
            return "该生产计划不存在，请选择其它生产计划重新排程";
        }
        if (poList.get(0).getStatus().equals(ProPlanStatus.COMPLETE.getStatus())){
            return "该生产计划已经全部完成,请选择其它生产计划重新排程";
        }
        //判断生产计划下的某个产品是否为已完成
        String status = orderDetailsPoMapper.getStatus(po.getProPlanId(),po.getProName());
        if (StringUtils.isEmpty(status)){
            return "修改失败,该生产计划单里没有此产品";
        }
        if (!status.equals("未完成")){
            return "该产品已经"+status+",请选择其它产品进行排程修改";
        }
        try {
            proSchedulingAdapter.updateProSche(po);
            return "修改成功";
        } catch (Exception e) {
            e.printStackTrace();
            return "修改失败"+e.getMessage();
        }
    }

    //提交生产排程
    public String updateStatus(Long proScheId,String status){
        try {
            proSchedulingAdapter.updateStatus(proScheId,status);
            return "操作成功";
        } catch (Exception e) {
            e.printStackTrace();
            return "操作失败"+e.getMessage();
        }
    }

}
